import spock.lang.Specification

/**
 * Created at 2017/10/16 17:19
 * @author gongweixin
 */
class OperatorExample extends Specification {

    def "test ? "() {
        expect:
        null == [:].a?.b
        false == [:].a?.startsWith("a") as boolean
        false == ([:].a?.size() == 2) as boolean
    }

    def "test + list"() {
        def dims = [:]
        dims["a"] = ["a"]
        dims["a"] = dims["a"] + "b"
        dims["a"] = dims["a"] + ["c", "d"]

        expect:
        [a:["a", "b", "c", "d"]] == dims
    }

    def "test + null"() {
        expect:
        5 == 5 + [:].name //空指针异常
    }

    def "test + different add"() {
        def a = [1,2]
        def b = [3,4]
        def c = a + b

        expect:
        [1,2] == a
        [1,2,3,4] == c
    }

    def "test null ?"() {
        expect:
        null == null?.groupBy {it.key}
        null == null?.groupBy {it.key}?.collect {it}
        [] == null?.groupBy {it.key}.collect {it}
        null == null?.collect {it}
        null == null?.collect {it}?.groupBy {it.key}
    }

    def "test obj + list"() {
        expect:
        [1, 2, 3] == 1 + [2, 3]
    }

    def "test set =="() {
        Set<Long> set1 = [2L, 1L, 3L]
        List<Long> list = [1L, 2L, 3L, 1L]
        Set<Long> set2 = list

        expect:
        set1 == set2
    }

    def "test map +"() {
        expect:
        [1: 1, 2: 2, 3: 3] == [1: 4, 2: 2] + [1: 1, 3: 3]
    }
}
